package bishi;

import java.time.LocalDate;
import java.util.*;

/**
 * 没用到下面的方法
 */
public class TradingDayCache {
    Map<Integer, Node> cache = new HashMap<>();

    public List<LocalDate> queryBetween(LocalDate from, LocalDate to) {
        if (from.compareTo(to) > 0) {
            System.out.println("开始时间不能大于结束时间");
            return null;
        }
        List<LocalDate> res;
        if (to.getYear() != from.getYear()) {
            res = cache.get(from.getYear()).queryBetween(from, LocalDate.of(from.getYear(), 12, 31));
            int nowYear = from.getYear() + 1;
            while (nowYear < to.getYear()) {
                res.addAll(cache.get(nowYear).queryBetween(LocalDate.of(nowYear, 1, 1), LocalDate.of(nowYear, 12, 31)));
                nowYear++;
            }
            res.addAll(cache.get(nowYear).queryBetween(LocalDate.of(nowYear, 1, 1), to));
        } else {
            return cache.get(from.getYear()).queryBetween(from, to);
        }
        return res;
    }

    public LocalDate getNext(LocalDate date) {
        LocalDate res = cache.get(date.getYear()).getNext(date);
        if (res == null) {
            //1月1日必定不是工作日！
            res = cache.get(date.getYear() + 1).getNext(LocalDate.of(date.getYear() + 1, 1, 1));
        }
        return res;
    }

    public LocalDate queryFirstTradingDayOfMonth(LocalDate date) {
        LocalDate of = LocalDate.of(date.getYear(), date.getMonth(), 1);
        if (cache.get(date.getYear()).isExist(of)) {
            return of;
        }
        return getNext(date);

    }

}
